package net.jxiny.oschina.synthetical.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.okhttplib.HttpInfo;
import com.okhttplib.OkHttpUtil;
import com.okhttplib.callback.Callback;
import com.youth.banner.Banner;

import net.jxiny.oschina.R;
import net.jxiny.oschina.net.URLList;
import net.jxiny.oschina.synthetical.Adapter.SoftwareAdapter;
import net.jxiny.oschina.synthetical.Common;
import net.jxiny.oschina.synthetical.NewsDetailActivity;

import net.jxiny.oschina.synthetical.Result.SoftwareResult;

import net.jxiny.oschina.synthetical.enyity.Software;
import net.jxiny.oschina.util.ACache;
import net.jxiny.oschina.util.GlideImageLoader;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class SoftwareFragemnt extends Fragment implements SwipeRefreshLayout.OnRefreshListener {
    private Unbinder binder;
    @BindView(R.id.recyclerView_software)
    RecyclerView SoftwareView;
    private List<Software> lists = new ArrayList<>();
    ArrayList<Integer>imges= new ArrayList<>();
    private SoftwareAdapter mSoftwareAdapter;

    @BindView(R.id.refresh)
    SwipeRefreshLayout refreshLayout;


    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_software, container, false);
        binder = ButterKnife.bind(this, view);

        refreshLayout.setOnRefreshListener(this);
        return view;
    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initData();

    }

    private void initData() {


        OkHttpUtil.getDefault(this)
                .doGetAsync(HttpInfo.Builder().setUrl(URLList.GET_SOFTWARE +
                        ACache.get(getActivity()).getAsString("token")+"&catalog=1").build(), new Callback() {
                    @Override
                    public void onSuccess(HttpInfo info) throws IOException {

                        SoftwareResult result = info.getRetDetail(SoftwareResult.class);

                        Common.beans=result.getProjectlist();
                        mSoftwareAdapter.replaceData(result.getProjectlist());
                    }
                    @Override
                    public void onFailure(HttpInfo info) throws IOException {

                        Toast.makeText(getContext(),"发送失败"+info,Toast.LENGTH_LONG).show();
                    }
                });
        //*********************************************
        RecyclerView.LayoutManager manager = new LinearLayoutManager(getActivity(),RecyclerView.VERTICAL,false);
        SoftwareView.setLayoutManager(manager);
        mSoftwareAdapter = new SoftwareAdapter(R.layout.item_software,lists);
        SoftwareView.setAdapter(mSoftwareAdapter);

        mSoftwareAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(getContext(), NewsDetailActivity.class);
                Bundle bundle = new Bundle();
                bundle.putInt("k",position);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });


        //处理Header
        View view1 = View.inflate(getActivity(),R.layout.sbanner,null);
        Banner banner = view1.findViewById(R.id.banner3);
        banner.setImageLoader(new GlideImageLoader());
        banner.isAutoPlay(false);

        imges.add(R.mipmap.pic1);
        imges.add(R.mipmap.pic2);
        imges.add(R.mipmap.pic3);
        //设置图片集合
        banner.setImages(imges);
        //banner设置方法全部调用完毕时最后调用
        banner.start();
        mSoftwareAdapter.addHeaderView(view1);

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binder.unbind();
    }

    @Override
    public void onRefresh() {
        initData();

        refreshLayout.postDelayed(new Runnable() {
            @Override
            public void run() {

                refreshLayout.setRefreshing(false);
            }
        },3000);



    }
}
